<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: configExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('configExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">configExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Example program demonstrating the use of ArConfigThis program shows how to use <a class="el" href="classArConfig.html" title="Stores configuration information which may be read to and from files or other sources. ">ArConfig</a> to store configuration parameters and load/save them from a file.</p>
<p>The ArNetworking library includes server classes that will let you use a remote client such as MobileEyes to view and change the configuration. See ArNetworking documentation and examples.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">class </span>ConfigExample</div><div class="line">{</div><div class="line">  <a name="_a0"></a><a class="code" href="classArConfig.html">ArConfig</a>* myConfig;</div><div class="line">  <span class="keywordtype">int</span> myIntParam;</div><div class="line">  <span class="keywordtype">double</span> myDoubleParam;</div><div class="line">  <span class="keywordtype">bool</span> myBoolParam;</div><div class="line">  <span class="keywordtype">char</span> myStringParam[256];</div><div class="line">  <a name="_a1"></a><a class="code" href="classArRetFunctorC.html">ArRetFunctorC&lt;bool, ConfigExample&gt;</a> myProcessConfigCB;</div><div class="line"></div><div class="line"><span class="keyword">public</span>:</div><div class="line">  ConfigExample():</div><div class="line">    myIntParam(0),</div><div class="line">    myDoubleParam(0.5),</div><div class="line">    myBoolParam(<span class="keyword">false</span>),</div><div class="line">    myProcessConfigCB(<span class="keyword">this</span>, &amp;ConfigExample::processConfigFile)</div><div class="line">  {</div><div class="line">    <span class="comment">// The global Aria class contains an ArConfig object.  You can create</span></div><div class="line">    <span class="comment">// other instances of ArConfig, but this is how you can share one ArConfig</span></div><div class="line">    <span class="comment">// among various program modules.</span></div><div class="line">    <span class="comment">// If you want to store a config parameter in ArConfig, first you must add </span></div><div class="line">    <span class="comment">// it to the ArConfig object.  Parameters are stored in sections, and</span></div><div class="line">    <span class="comment">// they affect a variable via a pointer provided in an ArConfigArg</span></div><div class="line">    <span class="comment">// object:</span></div><div class="line">    <a class="code" href="classArConfig.html">ArConfig</a>* config = <a name="a2"></a><a class="code" href="classAria.html#ace50b4190f04bcacaf285bb5830c5fc2">Aria::getConfig</a>();</div><div class="line">    config-&gt;<a name="a3"></a><a class="code" href="classArConfig.html#af464029663cdae8d46c20ad96536c801">setSectionComment</a>(<span class="stringliteral">&quot;Example Section&quot;</span>, <span class="stringliteral">&quot;Contains parameters created by the configExample&quot;</span>);</div><div class="line"></div><div class="line">    <span class="comment">// Add an integer which ranges from -10 to 10:</span></div><div class="line">    config-&gt;<a name="a4"></a><a class="code" href="classArConfig.html#ad3aa76dcf6fd83874dda8ccb577415e0">addParam</a>( <a name="_a5"></a><a class="code" href="classArConfigArg.html">ArConfigArg</a>(<span class="stringliteral">&quot;ExampleIntegerParameter&quot;</span>, &amp;myIntParam, <span class="stringliteral">&quot;Example parameter integer.&quot;</span>, -10, 10), <span class="stringliteral">&quot;Example Section&quot;</span>, <a name="a6"></a><a class="code" href="classArPriority.html#aee18574a3aae3c790bd3dadc13cff9a3a11a1e48113dade987dbbc50383e13210">ArPriority::NORMAL</a>);</div><div class="line">    </div><div class="line">    <span class="comment">// Add a floating point number which ranges from 0.0 to 1.0:</span></div><div class="line">    config-&gt;<a class="code" href="classArConfig.html#ad3aa76dcf6fd83874dda8ccb577415e0">addParam</a>( <a class="code" href="classArConfigArg.html">ArConfigArg</a>(<span class="stringliteral">&quot;ExampleDoubleParameter&quot;</span>, &amp;myDoubleParam, <span class="stringliteral">&quot;Example double precision floating point number.&quot;</span>, 0.0, 1.0), <span class="stringliteral">&quot;Example Section&quot;</span>, <a class="code" href="classArPriority.html#aee18574a3aae3c790bd3dadc13cff9a3a11a1e48113dade987dbbc50383e13210">ArPriority::NORMAL</a>);</div><div class="line"></div><div class="line">    <span class="comment">// Essential parameters can be placed in the &quot;Important&quot; priority level:</span></div><div class="line">    config-&gt;<a class="code" href="classArConfig.html#ad3aa76dcf6fd83874dda8ccb577415e0">addParam</a>( <a class="code" href="classArConfigArg.html">ArConfigArg</a>(<span class="stringliteral">&quot;ExampleBoolParameter&quot;</span>, &amp;myBoolParam, <span class="stringliteral">&quot;Example boolean parameter.&quot;</span>), <span class="stringliteral">&quot;Example Section&quot;</span>, <a name="a7"></a><a class="code" href="classArPriority.html#aee18574a3aae3c790bd3dadc13cff9a3af836d71b4aed41066c4e944d58177210">ArPriority::IMPORTANT</a>);</div><div class="line"></div><div class="line">    <span class="comment">// Unimportant parameters can be placed in the &quot;Trivial&quot; priority level:</span></div><div class="line">    myStringParam[0] = <span class="charliteral">&#39;\0&#39;</span>;  <span class="comment">// make string empty</span></div><div class="line">    config-&gt;<a class="code" href="classArConfig.html#ad3aa76dcf6fd83874dda8ccb577415e0">addParam</a>( <a class="code" href="classArConfigArg.html">ArConfigArg</a>(<span class="stringliteral">&quot;ExampleStringParameter&quot;</span>, myStringParam, <span class="stringliteral">&quot;Example string parameter.&quot;</span>, 256), <span class="stringliteral">&quot;Example Section&quot;</span>, <a name="a8"></a><a class="code" href="classArPriority.html#aee18574a3aae3c790bd3dadc13cff9a3a40f7bd43e779685d15dbfbfc712fd94e">ArPriority::TRIVIAL</a>);</div><div class="line"></div><div class="line">    <span class="comment">// You can set a callback to be invoked when the configuration changes, in</span></div><div class="line">    <span class="comment">// case you need to respond to any changes in the parameter values:</span></div><div class="line">    config-&gt;<a name="a9"></a><a class="code" href="classArConfig.html#ab6632033992fd48cb2369350a749f0b8">addProcessFileCB</a>(&amp;myProcessConfigCB, 0);</div><div class="line">  }</div><div class="line"></div><div class="line">  </div><div class="line">  <span class="comment">// Method called by config process callback when a new file is loaded.</span></div><div class="line">  <span class="comment">// It can return false to indicate an error, or true to indicate no error.</span></div><div class="line">  <span class="keywordtype">bool</span> processConfigFile() </div><div class="line">  {</div><div class="line">    <a name="a10"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a11"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;configExample: Config changed. New values: int=%d, float=%f, bool=%s, string=\&quot;%s\&quot;.&quot;</span>, myIntParam, myDoubleParam, myBoolParam?<span class="stringliteral">&quot;true&quot;</span>:<span class="stringliteral">&quot;false&quot;</span>, myStringParam);</div><div class="line">    <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">  }</div><div class="line">};</div><div class="line">  </div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">  <a name="a12"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line">  <a name="_a13"></a><a class="code" href="classArArgumentParser.html">ArArgumentParser</a> argParser(&amp;argc, argv);</div><div class="line">  argParser.<a name="a14"></a><a class="code" href="classArArgumentParser.html#a501694b62a7f8b0c3cadd84aeafe1b35">loadDefaultArguments</a>();</div><div class="line">  <span class="keywordflow">if</span> (argc &lt; 2 || !<a name="a15"></a><a class="code" href="classAria.html#a1b090c01d88bb420b8cf8e0384d25ee1">Aria::parseArgs</a>() || argParser.<a name="a16"></a><a class="code" href="classArArgumentParser.html#a08d0d87154763e4869f0342a47059886">checkArgument</a>(<span class="stringliteral">&quot;-help&quot;</span>))</div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a17"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;configExample usage: configExample &lt;config file&gt;.\nFor example, \&quot;configExample examples/configExample.cfg\&quot;.&quot;</span>);</div><div class="line">    <a name="a18"></a><a class="code" href="classAria.html#a10d71f3d4d0cf7f38c58a1f749f64a42">Aria::logOptions</a>();</div><div class="line">    <a name="a19"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(1);</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line">  </div><div class="line">  <span class="comment">// Object containing config parameters, and responding to changes:</span></div><div class="line">  ConfigExample configExample;</div><div class="line"></div><div class="line">  <span class="comment">// Load a config file given on the command line into the global </span></div><div class="line">  <span class="comment">// ArConfig object kept by Aria.  Normally ArConfig expects config</span></div><div class="line">  <span class="comment">// files to be in the main ARIA directory (i.e. /usr/local/Aria or</span></div><div class="line">  <span class="comment">// a directory specified by the $ARIA environment variable).</span></div><div class="line">  <span class="keywordtype">char</span> error[512];</div><div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span>* filename = argParser.<a name="a20"></a><a class="code" href="classArArgumentParser.html#a47aa47b8a59cdea0142a728db11ccbf9">getArg</a>(1);</div><div class="line">  <a class="code" href="classArConfig.html">ArConfig</a>* config = <a class="code" href="classAria.html#ace50b4190f04bcacaf285bb5830c5fc2">Aria::getConfig</a>();</div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;configExample: loading configuration file \&quot;%s\&quot;...&quot;</span>, filename);</div><div class="line">  <span class="keywordflow">if</span> (! config-&gt;<a name="a21"></a><a class="code" href="classArConfig.html#a3e14491304e22ded38de8ffcc203035a">parseFile</a>(filename, <span class="keyword">true</span>, <span class="keyword">false</span>, error, 512) )</div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;configExample: Error loading configuration file \&quot;%s\&quot; %s. Try \&quot;examples/configExample.cfg\&quot;.&quot;</span>, filename, error);</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(-1);</div><div class="line">    <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;configExample: Loaded configuration file \&quot;%s\&quot;.&quot;</span>, filename);</div><div class="line">  </div><div class="line">  <a name="_a22"></a><a class="code" href="classArConfigSection.html">ArConfigSection</a>* section = config-&gt;<a name="a23"></a><a class="code" href="classArConfig.html#a4348c2978086ece1e47d90131f597aca">findSection</a>(<span class="stringliteral">&quot;Example Section&quot;</span>);</div><div class="line">  <span class="keywordflow">if</span> (section)</div><div class="line">  {</div><div class="line">    <a class="code" href="classArConfigArg.html">ArConfigArg</a>* arg = section-&gt;<a name="a24"></a><a class="code" href="classArConfigSection.html#a7ff19dd5dfd1157dc989c0f204ab0669">findParam</a>(<span class="stringliteral">&quot;ExampleBoolParameter&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (arg)</div><div class="line">    {</div><div class="line">      arg-&gt;<a name="a25"></a><a class="code" href="classArConfigArg.html#a59cb64c658cce9197878993342be6333">setBool</a>(!arg-&gt;<a name="a26"></a><a class="code" href="classArConfigArg.html#a6947bf502281e7f23d84f2a6bf11ff70">getBool</a>());</div><div class="line">      <span class="comment">// After changing a config value, you should invoke the callbacks:</span></div><div class="line">      <span class="keywordflow">if</span> (! config-&gt;<a name="a27"></a><a class="code" href="classArConfig.html#a37c26aa78c1ae5ff479d99fd049dc975">callProcessFileCallBacks</a>(<span class="keyword">false</span>, error, 512) )</div><div class="line">      {</div><div class="line">        <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;configExample: Error processing modified config: %s.&quot;</span>, error);</div><div class="line">      }</div><div class="line">      <span class="keywordflow">else</span></div><div class="line">      {</div><div class="line">        <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;configExample: Successfully modified config and invoked callbacks.&quot;</span>);</div><div class="line">      }</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// You can save the configuration as well:</span></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;configExample: Saving configuration...&quot;</span>);</div><div class="line">  <span class="keywordflow">if</span>(!config-&gt;<a name="a28"></a><a class="code" href="classArConfig.html#af3c8626c844828f2ce632cc7027a7a67">writeFile</a>(filename))</div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;configExample: Error saving configuration to file \&quot;%s\&quot;!&quot;</span>, filename);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// end of program.</span></div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;configExample: end of program.&quot;</span>);</div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
